<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }

        .container {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .error {
            color: red;
        }

        .shake {
            animation: shake 0.5s;
        }

        @keyframes shake {
            0% {
                transform: translateX(0);
            }
            25% {
                transform: translateX(-5px);
            }
            50% {
                transform: translateX(5px);
            }
            75% {
                transform: translateX(-5px);
            }
            100% {
                transform: translateX(0);
            }
        }

        .warning {
            background: cornsilk;
            padding: 2px;
            margin-bottom: 10px;
            font-size: 12px;
            line-height: 6px;
        }

        .entrance {
            background: white;
            border: 1px solid;
            border-radius: 10%;
            padding: 0 4px;
            margin: 0 4px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="warning">
        <p>⚠ 发送验证码需要添加小魅为好友</p>
        <p>也可以直接向小魅发送<span class="entrance">网页版</span>获取一键登录链接</p>
    </div>
    <form id="loginForm">
        <div>
            <label for="qq">请输入QQ号</label>
            <input type="text" id="qq" name="qq" pattern="\d*">
            <span id="qqError" class="error"></span>
        </div>
        <div>
            <label for="code">请输入验证码</label>
            <input type="text" id="code" name="code" pattern="\d*">
            <span id="codeError" class="error"></span>
        </div>
        <div>
            <button type="button" id="getVerification">获取验证码</button>
            <span id="getVerificationError" class="error"></span>
        </div>
        <div>
            <button type="submit" id="loginButton" disabled>登录</button>
        </div>
    </form>
</div>
<div class="shake"></div>

<script>
    $(document).ready(function () {
        let verificationSent = false;
        let cooldown = false;

        const qqElement = $('#qq');
        const codeElement = $('#code');

        $('#getVerification').click(function () {
            if (cooldown) return;

            let qq = qqElement.val();
            if (!qq) {
                qqElement.addClass('shake').css('border-color', 'red');
                $('#qqError').text('QQ号为空');
                setTimeout(() => qqElement.removeClass('shake'), 500);
                return;
            }

            $.post('{% url "send_verification" %}', {qq: qq}, function (response) {
                if (response.success) {
                    verificationSent = true;
                    $('#loginButton').prop('disabled', false); // 启用登录按钮
                    startCooldown($('#getVerification'), 10);
                } else {
                    $('#getVerificationError').text(response.error);
                }
            });
        });

        $('#loginForm').submit(function (e) {
            e.preventDefault();
            if (!verificationSent) return;

            let qq = qqElement.val();
            let code = codeElement.val();

            $.post('{% url "login" %}', {qq: qq, code: code}, function (response) {
                if (response.success) {
                    window.location.href = '{{ request.GET.next }}' || '/';
                } else {
                    codeElement.addClass('shake').css('border-color', 'red');
                    $('#codeError').text(response.error);
                    setTimeout(() => codeElement.removeClass('shake'), 500);
                }
            });
        });

        function startCooldown(button, seconds) {
            cooldown = true;
            let originalText = button.text();
            button.attr('disabled', true);
            let countdown = seconds;

            let interval = setInterval(() => {
                button.text(`${originalText} (${countdown})`);
                countdown -= 1;
                if (countdown < 0) {
                    clearInterval(interval);
                    button.text(originalText);
                    button.attr('disabled', false);
                    cooldown = false;
                }
            }, 1000);
        }

        qqElement.on('input', function () {
            $(this).css('border-color', '');
            $('#qqError').text('');
        });

        codeElement.on('input', function () {
            $(this).css('border-color', '');
            $('#codeError').text('');
        });
    });
</script>
</body>
</html>
